package io.renren.modules.sys.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zheok.testing.entity.*;
import com.zheok.testing.vo.CatagoryVo;
import com.zheok.testing.vo.Headers;
import com.zheok.testing.vo.LableVo;
import com.zheok.testing.vo.ParamJsonVo;
import io.renren.modules.app.service.*;
import io.renren.modules.sys.entity.SysUserEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ThymeleafSc {
    @Autowired
    private TemplateEngine templateEngine;
    @Autowired
    private CatagoryService catagoryService;
    @Autowired
    private ProductService productService;
    @Autowired
    private JournalismService journalismService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private BannerPtService bannerPtService;
    @Autowired
    private BusinessService businessService;
    @Value("${shops_path}")
    private String shops_path;
    @Value("${business_path}")
    private String business_path;
    @Value("${index_path}")
    private String index_path;
    @Value("${product_path}")
    private String product_path;

    public void createShopPurchaseDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<BusinessEntity> businesss = businessService.list();
        businesss.forEach(business -> {
            map.put("business", business);
            map.put("banners", banners);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            context.setVariables(map);
            //输出流
            File file = new File(business_path + "detail/");

            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +business.getId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("businessDetail", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }

        });

    }

    public void createShopPurchase() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<BusinessEntity> businesss = businessService.list(null);
        map.put("businesss", businesss);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(business_path);

        file.mkdirs();

        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "business.html"), "UTF-8")) {
            //生成html
            templateEngine.process("business", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createProductList() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        map.put("catagoryVos", tree);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "productList.html"), "UTF-8")) {
            //生成html
            templateEngine.process("productList", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createShopActivity() {
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<ActivityEntity> activitys = activityService.list(new QueryWrapper<ActivityEntity>().lambda()
                    .eq(ActivityEntity::getShopId, shop.getUserId()));
            map.put("activitys", activitys);
            map.put("shop", shop);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            List<BannerPtEntity> list = bannerPtService.list();
            //查询自己的Banner
            List<BannerEntity> shopbanner = bannerService.list(new QueryWrapper<BannerEntity>().lambda().eq(BannerEntity::getShopId, shop.getUserId()));
            if (shopbanner.isEmpty()) {
                map.put("banners", list);
            } else {
                map.put("banners", shopbanner);
            }
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "/activity/");

            file.mkdirs();

            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopActivity", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createShopActivityDetail() {

        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<ActivityEntity> activitys = activityService.list();
        activitys.forEach(activity -> {
            SysUserEntity shop = sysUserService.getById(activity.getShopId());
            map.put("activity", activity);
            map.put("shop", shop);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            //查询自己的Banner
            List<BannerEntity> shopbanner = bannerService.list(new QueryWrapper<BannerEntity>().lambda().eq(BannerEntity::getShopId, activity.getShopId()));
            if (shopbanner.isEmpty()) {
                map.put("banners", banners);
            } else {
                map.put("banners", shopbanner);
            }
            context.setVariables(map);
            if (shop != null) {
                //输出流
                File file = new File(shops_path + shop.getUserId() + "/activity/detail/");

                file.mkdirs();

                try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +activity.getId() + ".html"), "UTF-8")) {
                    //生成html
                    templateEngine.process("activityDetail", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            }

        });

    }

    public void createShopJournalismDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<JournalismEntity> Journalisms = journalismService.list();
        Journalisms.forEach(Journalism -> {
            SysUserEntity shop = sysUserService.getById(Journalism.getShopId());
            map.put("journalism", Journalism);
            map.put("shop", shop);
            map.put("banners", banners);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            context.setVariables(map);
            if (shop != null) {
                //输出流
                File file = new File(shops_path + shop.getUserId() + "/news/detail/");
                file.mkdirs();

                try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +Journalism.getId() + ".html"), "UTF-8")) {
                    //生成html
                    templateEngine.process("articleDetail", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            }

        });

    }

    public void createShopJournalism() {     //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<JournalismEntity> Journalisms = journalismService.list(new QueryWrapper<JournalismEntity>().lambda()
                    .eq(JournalismEntity::getShopId, shop.getUserId()));
            map.put("journalisms", Journalisms);
            map.put("shop", shop);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            //查询自己的Banner
            List<BannerEntity> shopbanner = bannerService.list(new QueryWrapper<BannerEntity>().lambda().eq(BannerEntity::getShopId, shop.getUserId()));
            if (shopbanner.isEmpty()) {
                map.put("banners", banners);
            } else {
                map.put("banners", shopbanner);
            }
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "/news/");
            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopNews", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createShopProduct() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //根据所有商户生成产品页面
        List<SysUserEntity> shops = sysUserService.list();
        shops.forEach(shop -> {
            List<ProductEntity> products = productService.list(new QueryWrapper<ProductEntity>().lambda()
                    .eq(ProductEntity::getShopId, shop.getUserId()));
            map.put("products", products);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);

            //查询自己的Banner
            List<BannerEntity> shopbanner = bannerService.list(new QueryWrapper<BannerEntity>().lambda().eq(BannerEntity::getShopId, shop.getUserId()));
            if (shopbanner.isEmpty()) {
                map.put("banners", banners);
            } else {
                map.put("banners", shopbanner);
            }
            map.put("shop", shop);

            products.forEach(productEntity -> {
                //根据当前产品查询参数
                List<ParamJsonVo> param = productService.getParam(productEntity.getId() + "");
                productEntity.setCatagory2(productEntity.getCatagoryId().split(",")[1]);
                productEntity.setParams(param);

            });
            context.setVariables(map);
            //输出流
            File file = new File(shops_path + shop.getUserId() + "/product/");

            file.mkdirs();

            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), +shop.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopProduct", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    public void createProduct() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        //所有企业 new QueryWrapper<SysUserEntity>().lambda().ne(SysUserEntity::getUserId,"1")
        List<SysUserEntity> shops = sysUserService.list();
        //拿到所有产品

        //拿到所有二级分类
        List<CatagoryVo> tree = catagoryService.tree();
        tree.forEach(item -> {
            //一级分类
            item.getChildren().forEach(item2 -> {

                Map<String, Object> catagorys = new HashMap<>();
                PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                        .eq(PlatformSettingsEntity::getTdk, "title"));
                catagorys.put("title",title);
                PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                        .eq(PlatformSettingsEntity::getTdk, "description"));
                catagorys.put("description",description);
                PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                        .eq(PlatformSettingsEntity::getTdk, "keywords"));
                catagorys.put("keywords",keywords);
                catagorys.put("catagroy1", item.getLabel());//一级分类名称
                catagorys.put("catagroy2", item2.getLabel());//二级分类名称
                catagorys.put("catagroyId", item2.getId());//二级分类名称
                List<LableVo> catagory2 = productService.getCatagory2(item2.getId());
                catagorys.put("catagoryLables", catagory2);//二级分类标签
                catagorys.put("catagory3", item2.getChildren());
                catagorys.put("banners", banners);
                catagorys.put("shops", shops);//所有商户
                List<ProductEntity> products = productService.list(
                        new QueryWrapper<ProductEntity>().lambda()
                                .like(ProductEntity::getCatagoryId, item2.getId()));
                products.forEach(pro -> {
                    SysUserEntity byId = sysUserService.getById(pro.getShopId());
                    pro.setShopName(byId == null ? "" : byId.getName());
                });
                catagorys.put("products", products);//当前二级分类下的所有产品
                context.setVariables(catagorys);
                //根据分类ID生成产品页
                File file = new File(product_path);
                file.mkdirs();
                try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), item2.getId() + ".html"), "UTF-8")) {
                    //生成html
                    templateEngine.process("product", context, printWriter);
                } catch (Exception e) {
                    log.error("[静态页服务] 生成静态页异常", e);
                }
            });

        });
    }

    public void createProductDetail() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        //4、新品 12 个
        List<ProductEntity> products = productService.list();


        products.forEach(item -> {

            //相关产品 //同一个分类
            List<ProductEntity> xgProduct = productService.list(new QueryWrapper<ProductEntity>().lambda()
                    .eq(ProductEntity::getCatagoryId, item.getCatagoryId()));
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            map.put("xgProduct", xgProduct);
            map.put("product", item);
            map.put("catagoryVos", tree);
            SysUserEntity byId = sysUserService.getById(item.getShopId());
            map.put("shop", byId);
            map.put("banners", banners);
            //根据当前产品查询参数
            List<ParamJsonVo> param = productService.getParam(item.getId() + "");
            map.put("params", param);
            context.setVariables(map);
            //输出流
            File file = new File(product_path + "detail/");
            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), item.getId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("detail", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }

        });

    }

    public void createIndex() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        List<CatagoryVo> tree = catagoryService.tree();
        //4、新品 12 个
        IPage<ProductEntity> page = new Page<>(1, 12);
        IPage<ProductEntity> productPgae = productService.page(page, null);
        productPgae.getRecords().forEach(item->{
            item.setShopLogo(sysUserService.getById(item.getShopId()).getLogo());
        });
//
//        List<ProductEntity> list = productService.list(new QueryWrapper<ProductEntity>().lambda()
//                .groupBy(ProductEntity::getShopId));

        //新闻 五个
        IPage<JournalismEntity> journalisms = journalismService.page(new Page<>(1, 5), null);
        //活动 四个
        IPage<ActivityEntity> activitys = activityService.page(new Page<>(1, 4), null);
        //采购
        IPage<BusinessEntity> business = businessService.page(new Page<>(1, 8), null);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        map.put("business", business.getRecords());
        map.put("activitys", activitys.getRecords());
        map.put("journalisms", journalisms.getRecords());
        map.put("products", productPgae.getRecords());
        map.put("catagoryVos", tree);
        map.put("banners", banners);
        List<SysUserEntity> shops = sysUserService.list();
        map.put("shops",shops);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "index.html"), "UTF-8")) {
            //生成html
            templateEngine.process("index", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    @Autowired
    private ShopinfoService shopinfoService;
    @Autowired
    private CaseService caseService;

    public void createNews() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个
        List<JournalismEntity> journalisms = journalismService.list();
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        map.put("journalisms", journalisms);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "news.html"), "UTF-8")) {
            //生成html
            templateEngine.process("news", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createhd() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个
        List<ActivityEntity> activitys = activityService.list();
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        map.put("activitys", activitys);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "activity.html"), "UTF-8")) {
            //生成html
            templateEngine.process("activity", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }





    /**
     * header.html
     */
    public void header() {

        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        PlatformSettingsEntity kh = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().
                lambda().
                eq(PlatformSettingsEntity::getTdk, "平台口号"));
        PlatformSettingsEntity pt400 = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().
                lambda().
                eq(PlatformSettingsEntity::getTdk, "平台400"));
        map.put("kh",kh);
        map.put("pt400",pt400);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "header.html"), "UTF-8")) {
            //生成html
            templateEngine.process("header", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页footer] 生成静态页异常", e);
        }
    }

    @Autowired
    private PlatformSettingsService platformSettingsService;
    /**
     * footer.html
     */
    public void footer() {

        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        PlatformSettingsEntity footer = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda().eq(PlatformSettingsEntity::getTdk, "footer"));
        map.put("footer",footer);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "footer.html"), "UTF-8")) {
            //生成html
            templateEngine.process("footer", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页footer] 生成静态页异常", e);
        }
    }

    /**
     * 采购
     */
    public void createCg() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个
        List<BusinessEntity> businesss = businessService.list();
        businesss.forEach(item -> {
            item.setShopLogo(sysUserService.getById(item.getShopId()) == null ? "" : sysUserService.getById(item.getShopId()).getLogo());
        });
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        map.put("businesss", businesss);
        map.put("banners", banners);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "business.html"), "UTF-8")) {
            //生成html
            templateEngine.process("business", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    /**
     * 展会介绍
     */
    public void createshop1() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个 info
        List<ShopinfoEntity> shopinfos = shopinfoService.list();
        shopinfos.forEach(item -> {
            List<CaseEntity> caseEntity = caseService.list(new QueryWrapper<CaseEntity>().lambda()
                    .eq(CaseEntity::getShopId, item.getShopId()));
            map.put("caseEntity", caseEntity);
            map.put("shop", sysUserService.getById(item.getShopId()));
            map.put("shopinfo", item);
            map.put("banners", banners);
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            context.setVariables(map);
            //输出流
            File file = new File(shops_path);
            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), item.getShopId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shop", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }
        });

    }

    @Autowired
    private BannerService bannerService;

    @Autowired
    private FilesService filesService;

    /**
     * 展会介绍
     */
    public void downFiles() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个 info
        List<FilesEntity> files = filesService.list();
        map.put("banners", banners);
        map.put("files", files);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "down.html"), "UTF-8")) {
            //生成html
            templateEngine.process("down", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }

    }


    /**
     * 联系方式
     */
    public void createshop() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        //1、平台首页Banner
        //2、平台联系方式
        //3、产品分类
        //4、新品 12 个 info
        List<SysUserEntity> shop = sysUserService.list();
        shop.forEach(item -> {

            ShopinfoEntity shopinfo = shopinfoService.getOne(new QueryWrapper<ShopinfoEntity>().lambda().eq(ShopinfoEntity::getShopId, item.getUserId()));
            if (shopinfo != null) {
                String json = shopinfo.getAdditional();
                JSONObject jsonObject = JSON.parseObject(json);

                String headers1 = JSON.toJSONString(jsonObject.get("headers"));
                List<Headers> headers = JSON.parseObject(headers1, new TypeReference<List<Headers>>() {
                });
                //吧JSon转为对
                map.put("headers", headers);
            }
            PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "title"));
            map.put("title",title);
            PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "description"));
            map.put("description",description);
            PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                    .eq(PlatformSettingsEntity::getTdk, "keywords"));
            map.put("keywords",keywords);
            map.put("shopinfo", shopinfo);
            map.put("shop", item);
            //查询自己的Banner
            List<BannerEntity> shopbanner = bannerService.list(new QueryWrapper<BannerEntity>().lambda().eq(BannerEntity::getShopId, item.getUserId()));
            if (shopbanner.isEmpty()) {
                map.put("banners", banners);
            } else {
                map.put("banners", shopbanner);
            }
            context.setVariables(map);
            //输出流
            File file = new File(shops_path);
            file.mkdirs();
            try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "shopInfor-" + item.getUserId() + ".html"), "UTF-8")) {
                //生成html
                templateEngine.process("shopInfor", context, printWriter);
            } catch (Exception e) {
                log.error("[静态页服务] 生成静态页异常", e);
            }

        });

    }

    public void test() {
        System.out.println("开始静态化");
        long start = System.currentTimeMillis();
        header();//平台头
        downFiles();
        createshop1();
        createshop();
        createIndex();//首页
        createProductDetail();//产品详情
        createProduct();//产品页
        createShopProduct();//商户产品
        createShopJournalism();//商户新闻
        createShopJournalismDetail();//新闻详情页
        createShopActivity();//商户活动
        createShopActivityDetail();//商户活动详情
        createShopPurchase();//采购页面
        createShopPurchaseDetail();//采购详情
        createProductList();//分类页面
        createNews();
        createCg();
        createhd();
        footer();
        createAbout();
        createjieshao();
        createLinks();
        createMedia();
        long end = System.currentTimeMillis();
        System.out.println("总耗时" + (end - start) + "ms");
    }



    @Autowired
    private LcooperationService lcooperationService;
    public void createMedia() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        List<LcooperationEntity> list = lcooperationService.list();
        map.put("list", list);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "mediaPartners.html"), "UTF-8")) {
            //生成html
            templateEngine.process("mediaPartners", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }

    public void createAbout() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        PlatformSettingsEntity settings = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda().eq(PlatformSettingsEntity::getTdk,"平台简介"));
        map.put("settings", settings);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "about.html"), "UTF-8")) {
            //生成html
            templateEngine.process("about", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }


    @Autowired
    private LinksService linksService;
    @Autowired
    private IntroduceService introduceService;

    public void createjieshao() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        List<IntroduceEntity> list = introduceService.list();
        map.put("list", list);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "jieshao.html"), "UTF-8")) {
            //生成html
            templateEngine.process("jieshao", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }



    public void createLinks() {
        //查询平台Banner
        List<BannerPtEntity> banners = bannerPtService.list();
        //上下文
        Context context = new Context();
        HashMap<String, Object> map = new HashMap<>();
        List<LinksEntity> list = linksService.list();
        map.put("list", list);
        map.put("banners", banners);
        PlatformSettingsEntity title = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "title"));
        map.put("title",title);
        PlatformSettingsEntity description = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "description"));
        map.put("description",description);
        PlatformSettingsEntity keywords = platformSettingsService.getOne(new QueryWrapper<PlatformSettingsEntity>().lambda()
                .eq(PlatformSettingsEntity::getTdk, "keywords"));
        map.put("keywords",keywords);
        context.setVariables(map);
        //输出流
        File file = new File(index_path);
        file.mkdirs();
        try (PrintWriter printWriter = new PrintWriter(new File(file.getPath(), "links.html"), "UTF-8")) {
            //生成html
            templateEngine.process("links", context, printWriter);
        } catch (Exception e) {
            log.error("[静态页服务] 生成静态页异常", e);
        }
    }
}
